package day240601;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentManagementSystem {
    String name;
    int age;
    String hometown;
    private static List<Student> students;
    public  StudentManagementSystem() {
        this.students=new ArrayList<>();
    }
    public void addStudent(Student student) {
        students.add(student);
    }
    public void removeStudent(String Name) {
        students.removeIf(student -> student.getName().equals(Name));
    }
    private static void updateStudent() {
        System.out.println("请输入要修改的学生姓名：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();

        Student student = findStudentByName(name);
        if (student == null) {
            System.out.println("学生不存在！");
            return;
        }

        System.out.println("请输入学生新姓名：");
        String newName = scanner.next();

        System.out.println("请输入学生新年龄：");
        int newAge = scanner.nextInt();

        System.out.println("请输入学生新家乡：");
        String newHometown = scanner.next();

        student.setName(newName);
        student.setAge(newAge);
        student.setHometown(newHometown);

        System.out.println("修改学生成功！");
    }

    private static void queryStudent() {
        System.out.println("请输入要查询的学生姓名：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();

        Student student = findStudentByName(name);
        if (student == null) {
            System.out.println("学生不存在！");
            return;
        }

        System.out.println(student);
    }

    private static Student findStudentByName(String name) {
        for (Student student : students) {
            if (student.getName().equals(name)) {
                return student;
            }
        }

        return null;
    }

    public void listStudents() {
        for (Student student : students) {
            System.out.println(student);
        }
    }
    public static void main(String[] args) {
        StudentManagementSystem system = new StudentManagementSystem();
        Scanner scanner = new Scanner(System.in);
            while (true) {

                System.out.println("1.列出所有学生信息");
                System.out.println("2.增加学生");
                System.out.println("3.删除学生");
                System.out.println("4.更改学生信息");
                System.out.println("5.查找特定学生信息");
                System.out.println("6.退出系统");

                int choice = scanner.nextInt();
                scanner.nextLine(); // Consume the newline

                switch (choice) {
                    case 1:
                        system.listStudents();
                        break;
                    case 2:

                        System.out.print("姓名: ");
                        system.name = scanner.nextLine();
                        System.out.print("年龄: ");
                        system.age = scanner.nextInt();
                        scanner.nextLine(); // Consume the newline
                        System.out.print("家乡: ");
                        system.hometown = scanner.nextLine();
                        system.addStudent(new Student(system.name, system.age, system.hometown));

                        break;

                    case 3:
                        System.out.print("姓名: ");
                        system.name = scanner.nextLine();
                        system.removeStudent(system.name);
                        break;
                    case 4:
                        queryStudent();
                        break;
                    case 5:
                        System.out.print("姓名: ");
                        system.name = scanner.nextLine();
                        findStudentByName(system.name);
                        break;
                    case 6:
                        System.out.println("退出系统");
                        scanner.close();
                        return;
                    default:
                        System.out.println("无效的选择，请重新选择。");
                        break;
                }
        }
    }
}
